<?php

namespace apiend\modules\dynamic\models;

use Yii;
use common\base\Model;
use common\models\dynamic\DynamicLike;

class DynamicLikeForm extends Model {

    /**
     * @name $user_id
     * @var integer 用户UID
     */
    public $user_id;

    /**
     * @name $dynamic_id
     * @var integer 动态ID
     */
    public $dynamic_id;

    /**
     * @return array validation rules
     */
    public function rules() {
        return [
            [['user_id', 'dynamic_id'], 'integer'],
            [['dynamic_id'], 'required'],
        ];
    }

    /**
     * 保存动态信息（发布&修改）
     * @param [type] $data
     * @return void
     */
    public function save() {
        if (!$this->validate()) {
            $this->dumpError('点赞操作参数校验失败', $this->getFirstErrors());
        }

        $model = DynamicLike::findOrNew($this->attributes);
        if (!$model->isNewRecord) {
            return !$model->delete();
        }
        return $model->save();
    }

}
